<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Store_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
	
	public function move_store($id, $storeid)
	{
		$data = array(
				'kho_id' => $storeid,
		);
	
		$this->db->where('id', $id);
		$this->db->update('vt_kho_item', $data);
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
	
	// Nhập đầu kỳ
	function nhapdauky($results)
	{
		$this->load->helper('date');	
		
		// Thực hiện add to hoa don
		$store_id = $this->input->post('store_id');
		foreach ($results as $result)
		{
			$soluong = $this->input->post('soluong_'.$result);
			$dongia = $this->input->post('dongia_'.$result);			
			$this->item_detail($store_id, $soluong, $dongia, $result);
				
		}
		//return $nhapxuat_id;
	}
	
	public function item_detail($store_id, $soluong, $dongia, $result)
	{
		$fields = array(
				'kho_id'		=> $store_id,
				'item_id'		=> $result,
				'sddk'			=> $soluong,
				'amount'		=> $soluong,
				'xuat_xu_id'	=> 1,
				'price'         => $dongia
		);
	
		$this->db->set($fields);
		$this->db->insert('vt_kho_item');
	}
	
	// get all list mon hoc from db
	function get_list_store($limit, $start)
	{
		$this->db->select('id, code, name, parent_id, description, manager_id, status');
		$this->db->from('vt_kho');
		$this->db->limit($limit, $start);
		$this->db->order_by('name', 'asc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	// get parent store
	function get_all_store()
	{
		$this->db->select('id, code, name, parent_id');
		$this->db->from('vt_kho');
		$this->db->order_by('name','asc');
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_items_by_store()
	{
		$storeid = $this->input->post('storeid');
		$this->db->select('vt_kho_item.*, vt_item.code as mavt, vt_item.name as tenvt, dvt.name as dvt, vt_kho_item.amount * vt_kho_item.price as thanhtien ');
		$this->db->from('vt_kho_item','vt_item','dvt');
		$this->db->join('vt_item','vt_item.id = vt_kho_item.item_id');
		$this->db->join('dvt', 'dvt.id = vt_item.id_dvt');
		$this->db->where('vt_kho_item.kho_id',$storeid);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function get_store_info()
	{
		$storeid = $this->input->post('storeid');
		$this->db->select('code, name');
		$this->db->from('vt_kho');
		$this->db->where('id',$storeid);
		$query = $this->db->get();
		return $query->row();
	}
	
	// add store
	function add_store()
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'code'			=> mb_strtoupper($this->input->post('code')),
				'description'	=> $this->input->post('description'),
				'parent_id'	=> $this->input->post('parent'),
				'manager_id'	=> $this->input->post('teacher'),
				'status'		=> 0,
		);
	
		$this->db->set($fields);
		$this->db->insert('vt_kho');
	}
	
	
	// Edit store
	public function edit_store($id)
	{
		$fields = array(
				'name'			=> $this->input->post('name'),
				'code'			=> mb_strtoupper($this->input->post('code')),
				'description'	=> $this->input->post('description'),
				'parent_id'	=> $this->input->post('parent'),
				'manager_id'	=> $this->input->post('teacher')
	
		);
	
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('vt_kho');
	}
	
	// get store
	function get_store($id)
	{
		$this->db->select('*');
		$this->db->from('vt_kho');
		$this->db->where('id',$id);
		$query = $this->db->get();
		return $query->row();
	}
	
	// count all category
	function count_all_store($status = '')
	{
		$this->db->select('id');
		$this->db->from('vt_kho');
		if($status != "")
		{
			$this->db->where('status',$status);
		}
		return $this->db->count_all_results();
	}
	// Lấy tất cả item đã nhập số dư đầu kỳ cho kho
	function get_all_sddked_item_by_storeid($id)
	{
		$this->db->select('vt_kho_item.*, vt_item.code, vt_item.name as item, dvt.name as dvt');
		$this->db->from('vt_kho_item');
		$this->db->join('vt_item', 'vt_item.id = vt_kho_item.item_id', 'left');
		$this->db->join('dvt', 'dvt.id = vt_item.id_dvt', 'left');
		$this->db->where('kho_id',$id);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	function saveiteminstore($id,$amount,$price)
	{
		$fields = array(
				'sddk'			=> $amount,
				'amount'			=> $amount,
				'price'			=> $price
		);
		
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('vt_kho_item');
	}
	
	function submit_item_in_store($id,$status=1)
	{
	
		$fields = array(
				'status'			=> $status,
		);
	
		$this->db->set($fields);
		$this->db->where('id', $id);
		return $this->db->update('vt_kho_item');
	}
	
	public function check_code_kho()
	{
		$code = $this->input->post('code');
		$query = "
		SELECT code
		FROM vt_kho WHERE code ='$code'
		";
		$result = $this->db->query($query);
		if($result->num_rows() >= 1)
		{
		return true;
		}
		else
		{
		return false;
		}
	}
	
	public function change_status($id, $status = 0)
	{
		$data = array(
				'status' => $status,
		);
	
		$this->db->where('id', $id);
		$this->db->update('vt_kho', $data);
		if($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}
}

/* Location: ./application/models/store_model.php */